﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services;
using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using System.Data;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// RawDataService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    public class RawDataService : System.Web.Services.WebService
    {
        [WebMethod]
        public List<RawDataTable> Enumerate()
        {
            SER.RawDtTableService service = new SER.RawDtTableService();

            List<RawDataTable> rawDataTableList = new List<RawDataTable>();

            ListResult<ENT.EDtTable> serResult = service.GetTables();
            if(serResult.IsSuccess)
            {
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    RawDataTable rawDataTable = new RawDataTable();
                    rawDataTable.Id = serResult.Data[index].Id;
                    rawDataTable.Name = serResult.Data[index].Name;
                    rawDataTable.Description = serResult.Data[index].Description;

                    rawDataTableList.Add(rawDataTable);
                }
            }
            return rawDataTableList;
        }

        [WebMethod]
        public List<RawDataTableColumn> GetSchema(Guid appId, string token, Guid tableId)
        {
            SER.RawDtTableService service = new SER.RawDtTableService();

            ListResult<ENT.EDtTableColumn> serResult = service.GetSchema(appId, token, tableId);

            List<RawDataTableColumn> columns = new List<RawDataTableColumn>();

            for (int index = 0; index < serResult.Data.Count; index++)
            {
                RawDataTableColumn column = new RawDataTableColumn();
                column.Id = serResult.Data[index].Id;
                column.Name = serResult.Data[index].Name;
                column.DbType = serResult.Data[index].Type;
                column.DbLength = serResult.Data[index].Length;
                column.DbNullable = serResult.Data[index].Nullable;

                columns.Add(column);
            }


            return columns;
        }

        [WebMethod]
        public System.Data.DataTable GetData(Guid appId, string token, Guid tableId, bool includeHistory)
        {
            SER.RawDtTableService service = new SER.RawDtTableService();

            ServiceResult<System.Data.DataTable> serResult = service.GetData(appId, token, tableId);

            return serResult.Data;
        }

        /// <summary>
        /// 反馈哪些数据被提取了
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="token"></param>
        /// <param name="tableId"></param>
        /// <param name="batchId">对方系统抽取数据时的批次号</param>
        /// <param name="dataIdList"></param>
        [WebMethod]
        public void Feedback(Guid appId, string token, Guid tableId, Guid batchId, int dataCount, Guid[] passedData, Guid[] unpassedData)
        {
            Logger.InfoToTag(Library.LogTAG, string.Format("{0} feedback table:{1}, count:{2}, pass:{3}, unpass:{4}", appId, tableId, dataCount, passedData.Length, unpassedData.Length));
            SER.AppExtractService aeService = new SER.AppExtractService();
            ServiceResult<ENT.EAppExtractRecord> aeResult = aeService.CreateOrUpdate(appId,
                batchId,
                Domain.DEF.DataServiceTypeId.RawData,
                tableId, 
                dataCount,
                passedData, 
                unpassedData);
            if(aeResult.IsSuccess)
            {
                Logger.ErrorToTag(Library.LogTAG, string.Format("{0} feedback table:{1}, count:{2}, pass:{3}, unpass:{4} success", appId, tableId, dataCount, passedData.Length, unpassedData.Length));
                //SER.RawDtTableService service = new SER.RawDtTableService();
                //service.CreateExtractHistory(appId, token, tableId, batchId, dataIdList);
            }
        }
    }
}
